/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-01-18
 * Time: 16:23
 */
 public class MySingleList {
   static class Node {
        //定于成内部类
        public int val; //存储的数据
        public Node next;//存储下一个节点的地址

        public Node(int val) {
            this.val = val;  //不给next,如果没存的话，不知道下一个节点的地址
        }
    }

    public Node head; //代表当前链表的头结点的引用

    public void createLink() {
        Node node1 = new Node(12);
        Node node2 = new Node(31);
        Node node3 = new Node(43);
        Node node4 = new Node(77);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        head = node1;
    }

    //遍历链表
    public void display() {
        Node cur = head;
        //遍历完之后,就不知道链表的head的头结点了 head==null了
        while (cur != null) {  //不能head.next!=null最后一个结点打印不到
            System.out.print(cur.val + " ");
            cur = cur.next; //head往后走
        }
        System.out.println();
    }
    public void display(Node ret) {
        Node cur = ret;
        while (cur != null) {  //不能head.next!=null最后一个结点打印不到
            System.out.print(cur.val + " ");
            cur = cur.next; //head往后走
        }
        System.out.println();
    }

    public void addLast(int data) {
        Node node = new Node(data);
        if (head == null) {
            head = node;
            return;
        }
        Node cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
        //如果没有节点,head为空,cur.next发生空指针异常
    }




}